package cn.lkx.serviceorder.service.impl;

import cn.lkx.commonutils.orderVo.OrderCourseVo;
import cn.lkx.commonutils.orderVo.OrderMemberVo;
import cn.lkx.serviceorder.client.EduClient;
import cn.lkx.serviceorder.client.UcenterClient;
import cn.lkx.serviceorder.entity.Order;
import cn.lkx.serviceorder.mapper.OrderMapper;
import cn.lkx.serviceorder.service.OrderService;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author lkx
 * @since 2022-05-12
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    EduClient eduClient;
    @Autowired
    UcenterClient ucenterClient;

    @Override
    public String saveOrder(String courseId, String memberId) {
        //远程调用根据会员id获取会员信息
        OrderMemberVo orderMemberVo = ucenterClient.getMemberToOrder(memberId);
        //远程调用根据课程id获取课程信息
        OrderCourseVo orderCourseVo = eduClient.getCourseInfoToOrder(courseId);

        //创建order 存入数据库
        Order order = new Order();
        String orderNo = IdWorker.getIdStr(); //随机生成订单号
        order.setOrderNo(orderNo); //订单号
        order.setStatus(0);  //支付状态  0未支付  1已支付
        order.setPayType(1);  //支付类型微信

        order.setCourseId(courseId);
        order.setCourseTitle(orderCourseVo.getTitle());
        order.setCourseCover(orderCourseVo.getCover());
        order.setTeacherName(orderCourseVo.getTeacherName());
        order.setTotalFee(orderCourseVo.getPrice());

        order.setMemberId(memberId);
        order.setMobile(orderMemberVo.getMobile());
        order.setNickname(orderMemberVo.getNickname());
        baseMapper.insert(order);
        return orderNo;
    }

    /*public static void main(String[] args) {
        String idStr = IdWorker.getIdStr();
        System.out.println(idStr);
    }*/
}
